package com.example.photograph.controller;

import com.example.photograph.domain.AjaxResult;
import com.example.photograph.domain.SysBag;
import com.example.photograph.domain.SysOrder;
import com.example.photograph.page.TableDataInfo;
import com.example.photograph.service.ISysBagService;
import com.example.photograph.service.ISysOrderItemService;
import com.example.photograph.service.ISysOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户订单Controller
 * 
 * @author wuyj
 * @date 2021-03-06
 */
@Controller
@RequestMapping("/photograph/order")
public class SysOrderController extends BaseController
{
    private String prefix = "photograph/order";

    @Autowired
    private ISysOrderService sysOrderService;
    @Autowired
    private ISysOrderItemService sysOrderItemService;
    @Autowired
    private ISysBagService sysBagService;

    @RequiresPermissions("photograph:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询用户订单列表
     */
    @RequiresPermissions("photograph:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysOrder sysOrder)
    {
        startPage();
        List<SysOrder> list = sysOrderService.selectSysOrderList(sysOrder);
        return getDataTable(list);
    }

    /**
     * 新增用户订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户订单
     */
    @RequiresPermissions("photograph:order:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysOrder sysOrder)
    {
        SysBag sysBag = sysBagService.selectSysBagById(sysOrder.getBagId());
        sysOrder.setDebtMoney(sysBag.getBagMoney());
        return toAjax(sysOrderService.insertSysOrder(sysOrder));
    }

    /**
     * 修改用户订单
     */
    @GetMapping("/edit/{orderId}")
    public String edit(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        SysOrder sysOrder = sysOrderService.selectSysOrderById(orderId);
        mmap.put("sysOrder", sysOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户订单
     */
    @RequiresPermissions("photograph:order:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysOrder sysOrder)
    {
        SysBag sysBag = sysBagService.selectSysBagById(sysOrder.getBagId());
        if (sysOrder.getPayMoney()>sysBag.getBagMoney()){
            return error("已付金额超过套餐金额！");
        }
        sysOrder.setDebtMoney(sysBag.getBagMoney()-sysOrder.getPayMoney());
        return toAjax(sysOrderService.updateSysOrder(sysOrder));
    }

    /**
     * 删除用户订单
     */
    @RequiresPermissions("photograph:order:remove")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysOrderService.deleteSysOrderByIds(ids));
    }

    /**
     * 订单分配员工
     */
    @RequiresPermissions("photograph:order:allot")
    @GetMapping("/allotUser/{orderId}")
    public String allotUser(@PathVariable("orderId") Long orderId, ModelMap mmap)
    {
        mmap.put("order", sysOrderService.selectSysOrderById(orderId));
        return prefix + "/allotUser";
    }

    /**
     * 保存订单分配员工
     */
    @RequiresPermissions("photograph:order:allot")
    @PostMapping("/addItem")
    @ResponseBody
    public AjaxResult addItem(Long orderId, Long[] userIds)
    {
        return toAjax(sysOrderItemService.updateOrderItem(orderId,userIds));
    }
}
